<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\ShippingTemplates;
use App\Models\ShippingTemplatesFree;
use App\Models\ShippingTemplatesRegion;
use App\Models\SystemCity;
use App\Traits\Controllers\AdminTraits;
use App\Transformers\Shipping\ShippingTemplatesInfoTransformer;
use App\Transformers\Shipping\ShippingTemplatesTransformer;
use App\Transformers\System\SystemCityTransformer;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class FreightTemplateController extends Controller
{
    use AdminTraits;

    public $model = ShippingTemplates::class;
    public $transformer = ShippingTemplatesTransformer::class;

    //返回所有的省份
    public function address()
    {
        $address = SystemCity::query()
            ->pimp()
            ->paginate($this->getPageLimit());

        return $this->response->paginator($address, new SystemCityTransformer());

    }


    //重写添加
    public function store( Request $request)
    {
        // 传入的数据进行验证
        $this->validateData($request);

        //将提交的数据填充到本模型的表，进行保存
        $model = $this->getQuery()->fill($request->all());
        $model->save();
        //new 一个新的关联的运费模板指定城市运费表
        $free = new ShippingTemplatesFree();
        //将本模型表生成的当前的id 作为关联模型的temp_id追加进去
        $free->offsetSet('temp_id', $model->id);
        //给关联表填充数据
        $free->fill($request->all());
        $free->save();
          //如果指定包邮
        if ($request->appoint == 1){
            //new 一个新的运费模板指定包邮关联表
            $region = new ShippingTemplatesRegion();
            //将本模型表生成的当前的id 作为关联模型的temp_id追加进去
            $region->offsetSet('temp_id', $model->id);
            //给关联表填充数据
            $region->fill($request->all());
            $region->save();

        }
        //指定送达开关
        if ($request->is_go == true){

            return $this->Province($request->province_id);
        }
        return $this->response->noContent();
    }

    public function Province($porvince_id){

         $province = SystemCity::query()
             ->where('province_id',$porvince_id)
             ->first();
         return $province;
    }

    //先返回要修改的数据
    public function oneShow(ShippingTemplates $shippingTemplates,){

        $model = $this->getQuery()
            ->with('free')
            ->with('region')
            ->find($shippingTemplates->id);

        return $this->response->item($model,new ShippingTemplatesInfoTransformer());
    }


    //模板修改
    public function TemplateUpdate(ShippingTemplates $shippingTemplates, Request $request)
    {

        //修改传进来的参数
        $shippingTemplates->fill($request->all());
        $shippingTemplates->save();

        //关联标的修改
        $free = new ShippingTemplatesFree();
        //修改运费模板指定城市运费表
        $free->where('temp_id',$shippingTemplates->id)->delete();
        $free->offsetSet('temp_id',$shippingTemplates->id);
        $free->fill($request->all());
        $free->save();
        //修改运费模板指定包邮关联表
        $region  = new ShippingTemplatesRegion();
        $region->where('temp_id',$shippingTemplates->id)->delete();
        $region->offsetSet('temp_id',$shippingTemplates->id);
        $region->fill($request->all());
        $region->save();
        return $this->response->noContent();

    }

    public function TemplateDestroy(ShippingTemplates $shippingTemplates)
    {

       $shippingTemplates->delete();
       //删除关联表里的数据
        $free = ShippingTemplatesFree::query()->where('temp_id',$shippingTemplates->id)->delete();
        //删除关联表里的数据
        $region = ShippingTemplatesRegion::query()->where('temp_id',$shippingTemplates->id)->delete();

        return $this->response->noContent();
    }


    /**
     * 参数验证规则
     *
     * @param Request $request
     * @return array
     */
    public function rules(Request $request): array
    {
        switch ($request->method()) {
            case 'GET':
                return [];
                break;
            case 'POST':
            case 'PUT':
            case 'PATCH':
                return [
                    'name' => 'required|max:255',
                    'appoint' => 'required',
                    'type' => 'required',


                ];
                break;

        }
        return [];
    }

    /**
     * 验证信息
     *
     * @return array
     */
    public function ruleMessages(): array
    {
        return [
            'name.required' => '模板名称不能为空',
            'name.max' => '名称最大不能超过255个字符',
            'appoint.required' => '是否指定包邮不能为空',
            'type.required' => '计费方式不能为空'
        ];
    }

}
